package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.guesstv.lib.module.GtDataProvider;
import com.stark.guesstv.lib.module.GtOptionGenerator;
import com.stark.guesstv.lib.module.bean.GuessOption;
import com.stark.guesstv.lib.module.bean.GuessSelOption;
import com.stark.guesstv.lib.module.bean.TvActorBean;
import com.stark.guesstv.lib.module.constant.GuessType;
import com.stark.guesstv.lib.module.util.GtPrefUtil;
import com.stark.guesstv.lib.ui.GuessImgView;

import java.util.List;

import flc.ast.R;
import flc.ast.adapter.OptionAdapter;
import flc.ast.adapter.SelOptionAdapter;
import flc.ast.databinding.ActivityGame2Binding;
import flc.ast.view.GuessPicPresenter;
import stark.common.basic.base.BaseMvpActivity;
import stark.common.basic.base.IView;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StatusBarUtils;

public class Game2Activity extends BaseMvpActivity<GuessPicPresenter, ActivityGame2Binding> implements GuessImgView {

    private OptionAdapter mOptionAdapter;
    private SelOptionAdapter mSelOptionAdapter;

    @Override
    protected int onCreate() {
        StatusBarUtils.setStatusBarTranslate(this);
        StatusBarUtils.setSystemStatusTextColor(false, this);
        return R.layout.activity_game2;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivGame2Back.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvSelOption.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        mSelOptionAdapter = new SelOptionAdapter();
        mSelOptionAdapter.setOnItemClickListener(this);
        mDataBinding.rvSelOption.setAdapter(mSelOptionAdapter);

        final int spanCount = 5;
        mDataBinding.rvOption.setLayoutManager(new GridLayoutManager(mContext, spanCount));
        mOptionAdapter = new OptionAdapter(spanCount);
        mOptionAdapter.setOnItemClickListener(this);
        mDataBinding.rvOption.setAdapter(mOptionAdapter);

        mDataBinding.tvGame2Last.setOnClickListener(this);
        mDataBinding.tvGame2Next.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvGame2Last://上一关
                mSelOptionAdapter.setType(0);
                mSelOptionAdapter.notifyDataSetChanged();
                mPresenter.next(false);
                break;
            case R.id.tvGame2Next://下一关
                mSelOptionAdapter.setType(0);
                mSelOptionAdapter.notifyDataSetChanged();
                mPresenter.next(true);
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter instanceof OptionAdapter) {
            if (mSelOptionAdapter.hasFillFull()) {
                return;
            }
            GuessOption option = mOptionAdapter.getItem(position);
            option.setSelected(true);
            mOptionAdapter.notifyItemChanged(position);
            mSelOptionAdapter.fillOption(option);

            //填充一个答案选项后，判断是否已填充满
            if (mSelOptionAdapter.hasFillFull()) {
                //判断答案是否正确
                String answer = mSelOptionAdapter.getSelAnswer();
                mPresenter.checkAnswer(answer);
            }
        } else if (adapter instanceof SelOptionAdapter) {
            mSelOptionAdapter.setType(0);
            mSelOptionAdapter.notifyDataSetChanged();
            GuessSelOption selOption = mSelOptionAdapter.getItem(position);
            if (selOption.hasFilled()) {
                selOption.option.setSelected(false);
                selOption.option = null;
                mSelOptionAdapter.notifyItemChanged(position);
                mOptionAdapter.notifyDataSetChanged();
            }
        }
    }

    @Override
    protected void initData() {
        mPresenter.showCurTvActor();
    }

    private void handleClickTip() {
        List<GuessSelOption> selOptions = mSelOptionAdapter.getValidData();
        GuessSelOption needPromptOption = null;
        for (GuessSelOption selOption : selOptions) {
            if (selOption.hasFilled()) {
                //已填的格子
                if (!selOption.isFillRight()) {
                    //没有填对
                    //下面这句是将其在选项列表中重新设为可见
                    selOption.option.setSelected(false);
                    selOption.option = null;
                    if (needPromptOption == null) {
                        needPromptOption = selOption;
                    }
                }
            } else {
                //还未填
                if (needPromptOption == null) {
                    needPromptOption = selOption;
                }
            }
        }

        if (needPromptOption != null) {
            //处理需要提示的词
            List<GuessOption> options = mOptionAdapter.getValidData();
            for (GuessOption option : options) {
                if (needPromptOption.rightContent.equals(option.content) && !option.isSelected()) {
                    needPromptOption.option = option;
                    option.setSelected(true);
                    break;
                }
            }
            mSelOptionAdapter.notifyDataSetChanged();
            mOptionAdapter.notifyDataSetChanged();
        }
        //添加完提示的词后，要检测是否已填满
        if (mSelOptionAdapter.hasFillFull()) {
            //判断答案是否正确
            String selAnswer = mSelOptionAdapter.getSelAnswer();
            mPresenter.checkAnswer(selAnswer);
        }
    }

    @Override
    protected GuessPicPresenter createPresenter() {
        int curPass = GtPrefUtil.getActorPass();
        List<TvActorBean> tvData = GtDataProvider.getActorData();
        return new GuessPicPresenter(GuessType.ACTOR, tvData.get(curPass));
    }

    @Override
    protected IView getIView() {
        return this;
    }

    @Override
    public void onShowCurTvActor(@NonNull TvActorBean bean, @NonNull List<GuessOption> options) {
        mDataBinding.ivGame2Img.setImageBitmap(GtDataProvider.getGuessImgBitmap(bean.imgName));
        mSelOptionAdapter.setNewInstance(GtOptionGenerator.generateEmptySelOptions(bean));
        mOptionAdapter.setNewInstance(options);
    }

    @Override
    public void onCompleteAllPass() {
        ToastUtils.showShort(R.string.gt_no_next_pass);
    }

    @Override
    public void onCheckAnswerRet(boolean right) {
        if (right) {
            mSelOptionAdapter.setType(1);
            mSelOptionAdapter.notifyDataSetChanged();
            ToastUtils.showShort(getString(R.string.answer_right));
            mDataBinding.tvGame2Next.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mPresenter.next(true);
                }
            }, 500);
        } else {
            mSelOptionAdapter.setType(2);
            mSelOptionAdapter.notifyDataSetChanged();
            ToastUtils.showShort(getString(R.string.answer_error));
        }
    }
}